package com.shengsheng.expschedule.service.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shengsheng.expschedule.base.result.ResultCodeEnum;
import com.shengsheng.expschedule.service.base.exception.ExpScheduleException;
import com.shengsheng.expschedule.service.ucenter.entity.Right;
import com.shengsheng.expschedule.service.ucenter.entity.Role;
import com.shengsheng.expschedule.service.ucenter.entity.Vo.RightInfoVo;
import com.shengsheng.expschedule.service.ucenter.entity.Vo.RightVo;
import com.shengsheng.expschedule.service.ucenter.mapper.RightMapper;
import com.shengsheng.expschedule.service.ucenter.mapper.RoleMapper;
import com.shengsheng.expschedule.service.ucenter.service.RightService;
import com.shengsheng.expschedule.service.ucenter.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 会员权限表 服务实现类
 * </p>
 *
 * @author yuan
 * @since 2021-11-04
 */
@Service
public class RightServiceImpl extends ServiceImpl<RightMapper, Right> implements RightService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleService roleService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveByRightVo(RightInfoVo rightVo) {

        Right right = new Right();
        BeanUtils.copyProperties(rightVo, right);
        // 判断要添加是一级权限
        if (rightVo.getRightParentId().equals("0")) {
            // 同步给超级管理员添加权限
            if (this.save(right)) return roleService.updateAdminRole(right.getId());
        } else return this.save(right);
        return false;
    }

    @Override
    public boolean removeRightById(String id) {

        // 判断当前权限是否被某个角色使用，如果使用了则提示无法删除
        // 查询角色对应的权限列表
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.select("right_ids");
        List<Role> roleList = roleMapper.selectList(roleQueryWrapper);
        for (Role role : roleList) {
            String rightIds = role.getRoleRightIds();
            // 如果字符串存在该 id，说明该角色拥有改权限，不能删除
            if (rightIds.contains(id)) throw new ExpScheduleException(ResultCodeEnum.RIGHT_DELETE_ERROR);
        }
        // 不存在该 id ，可以删除
        // 删除该分类下的所有数据
        List<RightVo> rightVoList = baseMapper.selectNestedListByParentId(id);
        List<String> rightIdList = new ArrayList<>();
        rightIdList.add(id);
        getRightIdListByNestedList(rightVoList, rightIdList);
        return this.removeByIds(rightIdList);
    }

    @Override
    public List<RightVo> getRightsNestedList() {
        return baseMapper.selectNestedList();
    }

    @Override
    public String getRightIdByRightName(String rightName) {

        QueryWrapper<Right> rightQueryWrapper = new QueryWrapper<>();
        rightQueryWrapper.eq("right_name", rightName)
                .select("id");
        return baseMapper.selectOne(rightQueryWrapper).getId();
    }

    /**
     * 根据权限嵌套对象，获取 包含权限 id 列表
     * @param rightVoList
     * @param rightIdList
     */
    public void getRightIdListByNestedList(List<RightVo> rightVoList, List<String> rightIdList) {

        for (RightVo rightVo : rightVoList) {
            rightIdList.add(rightVo.getId());
            if (rightVo.getChildren() != null) getRightIdListByNestedList(rightVo.getChildren(), rightIdList);
            else return;
        }
    }

}
